﻿// Allows the item or items of a container to be selected (row of a grid or list, tree node, etc)
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Selectable',

        internal: {

            initialize: function () {

                this.selector = null;

                this.selected = false;

                this.setSelectableClass('wjs-selectable');

                this.setSelectedClass('wjs-selected');
            }
        },

        setSelectableClass: function (cls) {

            if (this.selectableClass) {

                this.replaceClass(this.selectableClass, cls);
            }
            else {

                this.addClass(cls);
            }

            this.selectableClass = cls;

            return this;
        },

        setSelectedClass: function (cls) {

            this.selectedClass = cls;

            return this;
        },

        setSelector: function (selector) {
            var me = this;

            me.selector = selector;

            // Add the select on click event
            me.addEvents({

                click: function (evt) { // Select this view on click

                    me.select({
                        acceptChanges: false, // Set the field as modified
                        fireFieldChanged: true // Fire the field change event
                    });
                }

            });

            // Propagate through the children
            me.down().forEach(function (child) {

                if (child.setSelector) {

                    child.setSelector(selector);
                }
            });

        },

        clearSelector: function() {

            if (this.selected) {

                this.deselect(); // Clear selected
            }

            if (this.selectableClass) {

                this.removeClass(this.selectableClass);
            }
           
            this.selectedClass = null;

            // Remove the click event
            this.removeEvent('click');
        },

        select: function (options) {
            var selected;

            if (!this.selector) {

                return;
            }

            if (this.selector.beforeSelectHandler &&

               this.selector.beforeSelectHandler(this) === false) { // Cancel the selection if returned false

                return;
            }

            // For multiple selections, toogle on and off on click
            if (this.selected) {

                if (this.selector.multipleSelection) {

                    this.deselect();
                }

                return;
            }

            // For single selection, deselect the selected item if any
            if (!this.selector.multipleSelection) {

                selected = this.selector.getSelection()[0];

                if (selected) {

                    selected.deselect();
                }
            }

            // Make the selector select the view
            this.selector.select(this, options);

            this.selected = true;

            this.addClass(this.selectedClass);

            return this;
        },

        deselect: function () {

            if (!this.selector) {
                return;
            }

            if (!this.selected) {
                return;
            }

            // Make the selector deselect this view
            this.selector.deselect(this);

            this.selected = false;

            this.removeClass(this.selectedClass);
        },
    });

});